package com.ldc.wandroid.ui.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ldc.common.model.BaseModel;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.MyCollectAdapter;
import com.ldc.wandroid.databinding.ActivityMyCollectBinding;
import com.ldc.wandroid.model.MyCollectModel;
import com.ldc.wandroid.viewmodel.MyCollectViewModel;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

public class MyCollectActivity extends BaseMvvmActivity<ActivityMyCollectBinding, MyCollectViewModel> {


    //
    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, MyCollectActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    private int currIndex = 0;
    //
    private final MyCollectAdapter myCollectAdapter = new MyCollectAdapter();
    private final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.getCollect().removeObserver(this::getMyCollectResp);
    }

    @Override
    protected ActivityMyCollectBinding initViewBinding() {
        return ActivityMyCollectBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        viewBinding.topBar.lineBack.setOnClickListener(v -> finish());
        viewBinding.topBar.tvTitle.setText("我的收藏");
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        //
        viewBinding.rv.refreshView.setEnableRefresh(false);
        viewBinding.rv.refreshView.setOnLoadMoreListener(onLoadMoreListener);
        //
        initAdapter();
        viewModel.getCollect().observeForever(this::getMyCollectResp);
        viewModel.getMyCollectReq(currIndex);
    }


    public void getMyCollectResp(BaseModel<MyCollectModel> dt) {
        if (dt.isSuccess()) {
            if (currIndex == 0) {
                myCollectAdapter.setNewData(dt.getData().getDatas());
            } else {
                myCollectAdapter.addNewData(dt.getData().getDatas());
            }
        } else {
            ToastUtils.showShort(dt.getErrorMsg());
        }
    }

    public void unSelectCollectResp(BaseModel<Object> dt) {
        if (null == dt) {
            return;
        }
        if (dt.isSuccess()) {
            currIndex = 0;
            viewModel.getMyCollectReq(currIndex);
        }
    }


    // 适配器
    private void initAdapter() {
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setItemViewCacheSize(10);
        viewBinding.rv.recyclerView.setLayoutManager(layoutManager);
        viewBinding.rv.recyclerView.setAdapter(myCollectAdapter);


    }

    //上拉刷新
    private final OnLoadMoreListener onLoadMoreListener = refreshLayout -> {
        currIndex += 1;
        viewModel.getMyCollectReq(currIndex);

    };

    @Override
    protected MyCollectViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, MyCollectViewModel.class);
    }
}
